import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 11:26:14 AM
 * To change this template use File | Settings | File Templates.
 */


public class DSDMain
{

    public DSDMain()
    {
    }

    public static void main(String args[])
    {
        MyHandler myhandler = new MyHandler();
        Validator validator = new Validator(myhandler);
        XMLOutputter xmloutputter = new XMLOutputter(Format.getRawFormat().setEncoding("ISO-8859-1"));
        String s = null;
        String s1 = null;
        Object obj = null;
        Object obj1 = null;
        Object obj2 = null;
        long l1 = 0L;
        try
        {
            int i = 0;
            if(args.length > i && !args[i].equals("--help"))
            {
                if(args.length > i)
                    s = args[i++];
                if(args.length > i)
                    s1 = args[i++];
            }
            if(args.length == 0 || args.length > i || s == null)
            {
                System.out.println("DSD 2.0 Validator\nCopyright (C) 2002-2005 BRICS\n\nUsage: dsd <instance document URL> [ <DSD document URL> ]\n\nSee http://www.brics.dk/DSD/ for more information about DSD 2.0.");
                System.exit(-1);
            }
            long l = System.currentTimeMillis();
            File file = new File(System.getProperty("user.dir"));
            URL url = file.toURL();
            URL url1 = new URL(url, s);
            System.err.println("Loading instance document " + s + "...");
            Validator _tmp = validator;
            org.jdom.Document document = Validator.makeDocument(url1);
            org.jdom.Document document1;
            if(s1 != null)
            {
                System.err.println("Loading schema document " + s1 + "...");
                Validator _tmp1 = validator;
                document1 = Validator.makeDocument(new URL(url, s1));
            } else
            {
                Validator _tmp2 = validator;
                String s2 = Validator.findDSDref(document);
                if(s2 == null)
                {
                    System.err.println("Error: no DSD specified.");
                    System.exit(-1);
                }
                System.err.println("Loading schema document " + s2 + "...");
                Validator _tmp3 = validator;
                document1 = Validator.makeDocument(new URL(url1, s2));
            }
            try
            {
                System.err.println("Parsing schema...");
                Schema schema = new Schema(document1);
                l1 = System.currentTimeMillis();
                System.err.println("Validating...");
                org.jdom.Document document2 = validator.process(document, schema);
                xmloutputter.output(document2, System.out);
                if(myhandler.errors == 0)
                    System.err.println("Document is valid!");
                else
                    System.err.println(myhandler.errors + " error" + (myhandler.errors <= 1 ? "" : "s") + " detected. See output document for details.");
            }
            catch(SchemaErrorException schemaerrorexception)
            {
                System.err.println("Fatal error in the DSD document.\nTry validating it with the meta-DSD: http://www.brics.dk/DSD/dsd2.dsd\n");
                schemaerrorexception.printStackTrace();
                System.exit(-1);
            }
            System.err.println("[loading+parsing time: " + (l1 - l) + "ms, " + "validating time: " + (System.currentTimeMillis() - l1) + "ms]");
        }
        catch(JDOMException jdomexception)
        {
            System.err.println(jdomexception.getMessage());
            System.exit(-1);
        }
        catch(MalformedURLException malformedurlexception)
        {
            System.err.print("Malformed URL: ");
            System.err.println(malformedurlexception.getMessage());
            System.exit(-1);
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
            System.exit(-1);
        }
        System.exit(0);
    }
}
